home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / esc.jar / com / extensibility / exv / ExtSchemaValidator.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-06-30  |  5.0 KB  |  209 lines

  1. package com.extensibility.exv;
  2.  
  3. import com.extensibility.xml.BaseDeclaration;
  4. import com.extensibility.xml.BaseExemplar;
  5. import com.extensibility.xml.BaseFlavor;
  6. import com.extensibility.xml.ParserException;
  7. import com.extensibility.xml.Schema;
  8. import com.extensibility.xml.SchemaIntf;
  9. import com.extensibility.xml.SchemaUtilities;
  10. import com.extensibility.xml.SchemaWriter;
  11. import com.extensibility.xml.URI;
  12. import java.io.StringWriter;
  13. import java.util.Enumeration;
  14. import java.util.ResourceBundle;
  15. import java.util.Vector;
  16.  
  17. public class ExtSchemaValidator {
  18.    static final String NOT_WELL_FORMED_MESSAGE = "Schema is not well-formed.";
  19.    static final String XML_RESOURCE_PKG = "com.extensibility.xml.rsc.";
  20.    ResourceBundle ParserErrorBundle;
  21.    ResourceBundle ErrorSituationBundle;
  22.    ResourceBundle dataTypeDisplayNames;
  23.    ResourceBundle XMLResources;
  24.    boolean isSchemaValid = true;
  25.    boolean isWellFormed = true;
  26.    String results;
  27.    URI baseUri;
  28.    SchemaIntf schema;
  29.    String schemaPath;
  30.    public String tstMessage;
  31.    Vector parseErrors;
  32.  
  33.    public ExtSchemaValidator() {
  34.       this.init();
  35.    }
  36.  
  37.    public void validate(String var1, String var2) {
  38.       URI var3 = new URI(var1);
  39.       if (!var3.exists()) {
  40.          this.isSchemaValid = false;
  41.          this.results = "File does not exist.";
  42.       } else {
  43.          this.validate(var3, var3, var2);
  44.       }
  45.    }
  46.  
  47.    public void validate(URI var1, URI var2, String var3, String var4) {
  48.       this.schemaPath = var4;
  49.       this.validate(var1, var2, var3);
  50.    }
  51.  
  52.    public void validate(URI var1, URI var2, String var3) {
  53.       try {
  54.          if (var3 == null) {
  55.             var3 = SchemaUtilities.getFlavor(var1, true);
  56.          }
  57.  
  58.          this.schema = new Schema(var2, var3);
  59.          this.schema.setSchemaPath(this.schemaPath);
  60.          this.schema.setPrintValidatedBy(true);
  61.          this.results = "";
  62.  
  63.          try {
  64.             this.schema.setShowErrors(true);
  65.             this.schema.parse(var1, var2, this.schema.getCount(), new Boolean(false));
  66.             this.schema.doSecondPassValidation(var1);
  67.          } catch (Throwable var9) {
  68.             this.isSchemaValid = false;
  69.             this.results = String.valueOf(String.valueOf(this.results).concat(String.valueOf(var9.getMessage()))).concat(String.valueOf("\r\n"));
  70.             return;
  71.          }
  72.  
  73.          if (this.schema != null) {
  74.             this.checkAll();
  75.             this.parseErrors = this.schema.getParseErrors();
  76.             int var4 = this.countDeclErrors();
  77.             int var5 = this.schema.getCount();
  78.             if (this.parseErrors.isEmpty() && var4 == 0) {
  79.                this.isSchemaValid = true;
  80.                this.schema.setPrintValidatedBy(true);
  81.                this.schema.setShowErrors(false);
  82.                StringWriter var11 = new StringWriter();
  83.                this.schema.write(var11);
  84.                this.results = String.valueOf(this.results).concat(String.valueOf(var11.toString()));
  85.             } else {
  86.                this.isSchemaValid = false;
  87.                this.schema.setPrintValidatedBy(false);
  88.                this.schema.setShowErrors(true);
  89.                StringWriter var6 = new StringWriter();
  90.  
  91.                try {
  92.                   this.schema.write(var6);
  93.                } catch (Exception var8) {
  94.                }
  95.  
  96.                this.results = String.valueOf(this.results).concat(String.valueOf(var6.toString()));
  97.             }
  98.          } else {
  99.             this.isSchemaValid = false;
  100.             this.isWellFormed = false;
  101.             this.results = String.valueOf(this.results).concat(String.valueOf("Schema is not well-formed."));
  102.          }
  103.  
  104.       } catch (Exception var10) {
  105.          this.isSchemaValid = false;
  106.          this.isWellFormed = false;
  107.          this.results = String.valueOf(String.valueOf(String.valueOf(String.valueOf(this.results).concat(String.valueOf("Schema is not well-formed."))).concat(String.valueOf(" "))).concat(String.valueOf(((Throwable)var10).getMessage()))).concat(String.valueOf("\r\n"));
  108.       }
  109.    }
  110.  
  111.    public String getResults() {
  112.       return this.results;
  113.    }
  114.  
  115.    public boolean isValid() {
  116.       return this.isSchemaValid;
  117.    }
  118.  
  119.    public boolean isWellFormed() {
  120.       return this.isWellFormed;
  121.    }
  122.  
  123.    private int countDeclErrors() {
  124.       int var1 = 0;
  125.       Enumeration var2 = this.schema.getDeclarations();
  126.  
  127.       while(var2.hasMoreElements()) {
  128.          BaseDeclaration var3 = (BaseDeclaration)var2.nextElement();
  129.          if (var3.hasErrors()) {
  130.             ++var1;
  131.          }
  132.       }
  133.  
  134.       return var1;
  135.    }
  136.  
  137.    public Vector getDeclErrors() {
  138.       Vector var1 = new Vector();
  139.       Enumeration var2 = this.schema.getDeclarations();
  140.  
  141.       while(var2.hasMoreElements()) {
  142.          BaseDeclaration var3 = (BaseDeclaration)var2.nextElement();
  143.          if (var3.hasErrors()) {
  144.             Enumeration var4 = var3.getErrors().elements();
  145.  
  146.             while(var4.hasMoreElements()) {
  147.                var1.addElement(var4.nextElement());
  148.             }
  149.          }
  150.       }
  151.  
  152.       return var1;
  153.    }
  154.  
  155.    public Vector getParseErrors() {
  156.       return this.parseErrors;
  157.    }
  158.  
  159.    public Vector getAllErrors() {
  160.       Vector var1 = new Vector();
  161.       if (this.parseErrors != null) {
  162.          Enumeration var2 = this.parseErrors.elements();
  163.  
  164.          while(var2.hasMoreElements()) {
  165.             var1.addElement(var2.nextElement());
  166.          }
  167.       }
  168.  
  169.       if (this.getDeclErrors() != null) {
  170.          Enumeration var3 = this.getDeclErrors().elements();
  171.  
  172.          while(var3.hasMoreElements()) {
  173.             var1.addElement(var3.nextElement());
  174.          }
  175.       }
  176.  
  177.       return var1;
  178.    }
  179.  
  180.    private void checkAll() {
  181.       Enumeration var1 = this.schema.getDeclarations();
  182.  
  183.       while(var1.hasMoreElements()) {
  184.          BaseDeclaration var2 = (BaseDeclaration)var1.nextElement();
  185.          boolean var3 = var2.hasErrors();
  186.          this.schema.checkForErrors(var2);
  187.       }
  188.  
  189.       this.schema.checkForErrors((BaseDeclaration)null);
  190.    }
  191.  
  192.    private void init() {
  193.       this.XMLResources = ResourceBundle.getBundle("com.extensibility.xml.rsc.XMLResources");
  194.       this.ParserErrorBundle = ResourceBundle.getBundle("com.extensibility.xml.rsc.ParserExceptions");
  195.       ParserException.setMessageBundle(this.ParserErrorBundle);
  196.       this.dataTypeDisplayNames = ResourceBundle.getBundle("com.extensibility.xml.rsc.DataTypes");
  197.       BaseFlavor.setDisplayNameBundle(this.dataTypeDisplayNames);
  198.       BaseFlavor.loadDisplayNames();
  199.       BaseDeclaration.setResources(this.XMLResources);
  200.       BaseExemplar.setResources(this.XMLResources);
  201.       SchemaWriter.setResources(this.XMLResources);
  202.       this.schemaPath = "";
  203.    }
  204.  
  205.    public void setSchemaPath(String var1) {
  206.       this.schemaPath = var1;
  207.    }
  208. }
  209.